<route lang="json5" type="page">
{
  style: {
    navigationStyle: 'custom',
    navigationBarTitleText: '',
    disableScroll: true, // 微信禁止页面滚动
    'app-plus': {
      bounce: 'none', // 禁用 iOS 弹性效果
    },
  },
}
</route>

<template>
  <PageLayout :navbarShow="false" backRouteName="index" routeMethod="pushTab">
    <view class="page-container">
      <view class="text-center">
        <image :src="compLogo" mode="aspectFit" class="logo"></image>
        <view class="title text-shadow">{{ compTitle || 'JEECG BOOT' }}</view>

        <!-- 快捷登录 -->
        <QuickLogin
          v-if="loginWay === 0"
          v-model:loading="loading"
          @switch-way="toggleLoginWay"
          @login-success="handleLoginSuccess"
        />

        <!-- 注册表单 -->
        <RegisterForm
          v-else-if="loginWay === 1"
          v-model:loading="loading"
          @switch-way="toggleLoginWay"
          @register-success="handleRegisterSuccess"
        />

        <!-- 短信登录 -->
        <SmsLogin
          v-else
          v-model:loading="loading"
          @switch-way="toggleLoginWay"
          @login-success="handleLoginSuccess"
        />
      </view>
      <wd-notify />
    </view>
  </PageLayout>
</template>

<script lang="ts" setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { useToast } from 'wot-design-uni'
import { http } from '@/utils/http'
import { useUserStore } from '@/store/user'
import { useParamsStore } from '@/store/page-params'
import { useRouter } from '@/plugin/uni-mini-router'

import { APP_ROUTE, APP_CONFIG, HOME_CONFIG_EXPIRED_TIME, HOME_PAGE } from '@/common/constants'
import { cache, getFileAccessHttpUrl } from '@/common/uitls'
import { userApi } from '@/api/api'

// 导入组件
import QuickLogin from './components/QuickLogin.vue'
import SmsLogin from './components/SmsLogin.vue'
import RegisterForm from './components/RegisterForm.vue'

defineOptions({
  name: 'login',
  options: {
    styleIsolation: 'shared',
  },
})

const router = useRouter()
const toast = useToast()
const userStore = useUserStore()
const paramsStore = useParamsStore()

// Use runtime-accessible path under the project's static folder
// Vite/webpack aliases like `@/static/...` may not resolve to a proper runtime URL for <image>
const defLogo = '/static/chat/dfs-logo-text-nor.png'
const loading = ref(false)
const loginWay = ref(0) // 0: 快捷登录，1：注册，2：短信登录
const compLogo = ref(defLogo)
const compTitle = ref('东方树茶油')

// 是否开启本地路由配置
const isLocalConfig = getApp().globalData.isLocalConfig

paramsStore.reset()

const toggleLoginWay = (val: number) => {
  loginWay.value = val
}

const handleLoginSuccess = () => {
  // 获取app配置
  appConfig()
  departConfig()
}

const handleRegisterSuccess = () => {
  // 注册成功后的处理
  console.log('注册成功')
}

// 部門配置
const departConfig = () => {
  const appQueryUser = () => {
    http
      .get(userApi.getUserInfo, {
        username: userStore.userInfo.username,
      })
      .then((res: any) => {
        if (res.success && res.result.length) {
          const result = res.result[0]
          userStore.editUserInfo({ orgCodeTxt: result.orgCodeTxt })
        }
      })
  }
  appQueryUser()
}

const appConfig = () => {
  if (isLocalConfig) {
    toast.success('登录成功!')
    router.pushTab({ path: HOME_PAGE })
  } else {
    http
      .get('/eoa/sysAppConfig/queryAppConfigRoute')
      .then((res: any) => {
        if (res.success) {
          cache(APP_ROUTE, res.result.route, HOME_CONFIG_EXPIRED_TIME)
          cache(APP_CONFIG, res.result.config, HOME_CONFIG_EXPIRED_TIME)
        }
        toast.success('登录成功!')
        router.pushTab({ path: HOME_PAGE })
      })
      .catch((err) => {
        console.error('获取应用配置失败:', err)
        toast.success('登录成功!')
        router.pushTab({ path: HOME_PAGE })
      })
  }
}

const loadConfig = () => {
  http.get('/eoa/sysAppConfig/queryAppConfig').then((res: any) => {
    if (res.success) {
      const info = res.result
      if (info) {
        compLogo.value = getFileAccessHttpUrl(info.appLogo) || defLogo
        compTitle.value = info.appTitle || 'JEECG-BOOT'
      } else {
        compLogo.value = defLogo
      }
    }
  })
}

const checkToken = () => {
  const { userInfo, clearUserInfo } = userStore
  if (userInfo.token) {
    if (+new Date() - userInfo.localStorageTime > 2 * 3600000) {
      // 超过2小时过期
      clearUserInfo()
    } else {
      router.pushTab({ path: HOME_PAGE })
    }
  } else {
    clearUserInfo()
  }
}

const checkAccount = () => {
  // 账号检查逻辑
}

onMounted(() => {
  // #ifdef APP-PLUS || H5
  checkToken()
  checkAccount()
  // #endif

  // @ts-ignore
  if (isLocalConfig === false) {
    loadConfig()
  }
})
</script>

<style lang="scss" scoped>
.page-container {
  padding: 0 20upx;
  padding-top: 100upx;
  position: relative;
  font-size: 15px;
  color: var(--UI-FG-0);

  .logo {
    width: 450upx;
    /* keep units consistent for uni-app; use upx for responsive sizing */
    height: 450upx;
  }

  .title {
    font-size: 58upx;
  }
}

.text-center {
  text-align: center;
}

.text-shadow {
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.1);
}
</style>
